<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

namespace app\adminapi\controller\crud;

use app\adminapi\controller\AuthController;
use app\services\crud\QuestionServices;
use think\facade\App;

/**
 * 题目管理控制器
 * Class Question
 * @package app\adminapi\controller\crud
 */
class Question extends AuthController
{
    /**
     * Question constructor.
     * @param App $app
     * @param QuestionServices $services
     */
    public function __construct(App $app, QuestionServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 获取题目列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $where = $this->request->getMore([
            ['question_no', ''],
            ['title', ''],
            ['type', ''],
            ['category_id', ''],
            ['status', ''],
        ]);

        return $this->success($this->services->getList($where));
    }

    /**
     * 获取题目详情
     * @param int $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function read($id)
    {
        if (!$id) {
            return $this->fail('缺少参数');
        }

        return $this->success($this->services->getInfo((int)$id));
    }

    /**
     * 创建题目
     * @return mixed
     */
    public function save()
    {
        $data = $this->request->postMore([
            ['question_no', ''],
            ['title', ''],
            ['type', 1],
            ['category_id', 0],
            ['status', 1],
            ['is_required', 1],
            ['sort', 0],
            ['remark', ''],
            ['options', []], // 选项数据
        ]);

        $this->validate($data, [
            'question_no|题目编号' => 'require',
            'title|题目内容' => 'require',
            'type|题目类型' => 'require|in:1,2',
            'options|题目选项' => 'require|array',
        ]);

        // 验证选项数据
        if (empty($data['options'])) {
            return $this->fail('请添加题目选项');
        }

        foreach ($data['options'] as $index => $option) {
            if (empty($option['option_content'])) {
                return $this->fail('选项内容不能为空');
            }

            // 解析题必须有解析内容
            if ($data['type'] == 1 && empty($option['analysis'])) {
                return $this->fail('解析题必须填写解析内容');
            }

            // 分值题必须有分值
            if ($data['type'] == 2 && !isset($option['score'])) {
                return $this->fail('分值题必须设置选项分值');
            }
        }

        $id = $this->services->create($data);

        return $this->success(['id' => $id], '创建成功');
    }

    /**
     * 更新题目
     * @param int $id
     * @return mixed
     */
    public function update($id)
    {
        if (!$id) {
            return $this->fail('缺少参数');
        }

        $data = $this->request->postMore([
            ['question_no', ''],
            ['title', ''],
            ['type', ''],
            ['category_id', ''],
            ['status', ''],
            ['is_required', ''],
            ['sort', ''],
            ['remark', ''],
            ['options', []], // 选项数据
        ]);

        // 过滤空值
        $data = array_filter($data, function ($value) {
            return $value !== '';
        });

        if (empty($data)) {
            return $this->fail('没有要更新的数据');
        }

        // 验证选项数据
        if (isset($data['options'])) {
            if (!is_array($data['options']) || empty($data['options'])) {
                return $this->fail('选项数据格式错误');
            }

            foreach ($data['options'] as $option) {
                if (empty($option['option_content'])) {
                    return $this->fail('选项内容不能为空');
                }
            }
        }

        $this->services->update((int)$id, $data);

        return $this->success('更新成功');
    }

    /**
     * 删除题目
     * @param int $id
     * @return mixed
     */
    public function delete($id)
    {
        if (!$id) {
            return $this->fail('缺少参数');
        }

        $this->services->delete((int)$id);

        return $this->success('删除成功');
    }

    /**
     * 修改题目状态
     * @param int $id
     * @return mixed
     */
    public function set_status($id)
    {
        if (!$id) {
            return $this->fail('缺少参数');
        }

        $status = $this->request->post('status', 1);

        $this->services->setStatus((int)$id, (int)$status);

        return $this->success($status == 1 ? '启用成功' : '禁用成功');
    }
}

